<?php
class Mcontact extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="contact";
    }
    
    public function listAll($amount, $start) {
        $this->db->limit($amount, $start);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function search($fullname, $email, $datefrom, $dateto, $amount, $start) {
        $f=$e=$d="'1=1'";
        if($fullname!="") $f="fullname like '%".$fullname."%'";
        if($email!="") $e="email like '%".$email."%'";
        if($datefrom!="" and $dateto!="") $d="con_date between '".$datefrom."' and '".$dateto."'";
        $str="select * from contact where $f and $e and $d limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($fullname, $email, $datefrom, $dateto) {
        $f=$e=$d="'1=1'";
        if($fullname!="") $f="fullname like '%".$fullname."%'";
        if($email!="") $e="email like '%".$email."%'";
        if($datefrom!="" and $dateto!="") $d="con_date between '".$datefrom."' and '".$dateto."'";
        $str="select count(*) as 'count' from contact where $f and $e and $d";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getById($id) {
        $this->db->where("con_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("con_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("con_id", $id);
        $this->db->delete($this->_table);
    }
}
?>
